Public Class ctlThreeStateButton
    Inherits System.Windows.Forms.UserControl

    Public Enum ThreeStateButtonState
        StateOne = 0
        StateTwo = -1
        StateThree = -2
    End Enum

    Private m_CurrentState As ThreeStateButtonState = ThreeStateButtonState.StateOne
    Private m_StateCount As Integer = 2 ' 1-3
    Public Event ButtonStateChanged(ByVal newState As ThreeStateButtonState)


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnButton As System.Windows.Forms.Button
    Friend WithEvents pnlStateOneColor As System.Windows.Forms.Panel
    Friend WithEvents pnlStateTwoColor As System.Windows.Forms.Panel
    Friend WithEvents pnlStateThreeColor As System.Windows.Forms.Panel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnButton = New System.Windows.Forms.Button()
        Me.pnlStateOneColor = New System.Windows.Forms.Panel()
        Me.pnlStateTwoColor = New System.Windows.Forms.Panel()
        Me.pnlStateThreeColor = New System.Windows.Forms.Panel()
        Me.SuspendLayout()
        '
        'btnButton
        '
        Me.btnButton.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.btnButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnButton.Name = "btnButton"
        Me.btnButton.Size = New System.Drawing.Size(56, 34)
        Me.btnButton.TabIndex = 0
        Me.btnButton.Text = "Button One"
        '
        'pnlStateOneColor
        '
        Me.pnlStateOneColor.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.pnlStateOneColor.Location = New System.Drawing.Point(8, 72)
        Me.pnlStateOneColor.Name = "pnlStateOneColor"
        Me.pnlStateOneColor.Size = New System.Drawing.Size(24, 24)
        Me.pnlStateOneColor.TabIndex = 1
        '
        'pnlStateTwoColor
        '
        Me.pnlStateTwoColor.BackColor = System.Drawing.Color.Gold
        Me.pnlStateTwoColor.Location = New System.Drawing.Point(40, 72)
        Me.pnlStateTwoColor.Name = "pnlStateTwoColor"
        Me.pnlStateTwoColor.Size = New System.Drawing.Size(24, 24)
        Me.pnlStateTwoColor.TabIndex = 2
        '
        'pnlStateThreeColor
        '
        Me.pnlStateThreeColor.BackColor = System.Drawing.Color.DarkSalmon
        Me.pnlStateThreeColor.Location = New System.Drawing.Point(72, 72)
        Me.pnlStateThreeColor.Name = "pnlStateThreeColor"
        Me.pnlStateThreeColor.Size = New System.Drawing.Size(24, 24)
        Me.pnlStateThreeColor.TabIndex = 3
        '
        'ctlThreeStateButton
        '
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.pnlStateThreeColor, Me.pnlStateTwoColor, Me.pnlStateOneColor, Me.btnButton})
        Me.Name = "ctlThreeStateButton"
        Me.Size = New System.Drawing.Size(56, 34)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton.Click
        m_CurrentState -= 1
        If m_CurrentState < ThreeStateButtonState.StateThree Then
            m_CurrentState = ThreeStateButtonState.StateOne
        End If

        If m_StateCount = 2 Then
            If m_CurrentState = ThreeStateButtonState.StateTwo Then
                m_CurrentState = ThreeStateButtonState.StateThree
            End If
        End If

        SetColorForState()
        RaiseEvent ButtonStateChanged(m_CurrentState)

    End Sub

    Private Sub SetColorForState()
        Select Case m_CurrentState
            Case ThreeStateButtonState.StateOne : btnButton.BackColor = pnlStateOneColor.BackColor
            Case ThreeStateButtonState.StateTwo : btnButton.BackColor = pnlStateTwoColor.BackColor
            Case ThreeStateButtonState.StateThree : btnButton.BackColor = pnlStateThreeColor.BackColor
        End Select
    End Sub

    Public Property ButtonText() As String
        Get
            Return btnButton.Text
        End Get
        Set(ByVal Value As String)
            btnButton.Text = Value
        End Set
    End Property

    Public Property StateCount() As Integer
        Get
            Return m_statecount
        End Get
        Set(ByVal Value As Integer)
            If Value = 0 Then Exit Property
            If Value > 3 Then Exit Property
            m_statecount = Value
        End Set
    End Property

    Public Property CurrentState() As ThreeStateButtonState
        Get
            Return m_CurrentState
        End Get
        Set(ByVal Value As ThreeStateButtonState)
            m_CurrentState = Value
            SetColorForState()
        End Set
    End Property

End Class
